// 商品的持久层方法
const goodsModel = require("../model/goodsModel");

//增加商品
module.exports.insert = async function (obj) {
    return await goodsModel.create(obj)
}

//删除商品
module.exports.delete = async function (id) {
    return await goodsModel.deleteOne({ _id: id })
}

//查询商品
module.exports.find = async function (searchObj, page) {
    let { current, size } = page;
    let total = await goodsModel.countDocuments(searchObj)
    let totalPage = Math.ceil(total / size);
    let rows = await goodsModel.find(searchObj).populate("adminId").populate("shopsId").limit(parseInt(size)).skip((current - 1) * size);
    return { current, size, total, totalPage, rows };
}

//修改商品
module.exports.update = async function (id, obj) {
    return await goodsModel.updateOne({ _id: id }, obj)
}

//查询所有商品(根据门店管理员id或者获取所有数据库商品)
module.exports.findAll = async function (id) {
    let data;
    if (id) {
        data = await goodsModel.find({ adminId: id }).populate("adminId").populate("shopsId")
    } else {
        data = await goodsModel.find().populate("adminId").populate("shopsId")
    }
    return data;
}